package com.xilai.cuteBoy.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xilai.cuteBoy.dao.CuteBoyUserMapper;
import com.xilai.cuteBoy.entity.CuteBoyUser;
import com.xilai.cuteBoy.entity.CuteBoyUserExample;
import com.xilai.cuteBoy.service.UserServiceInter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserServiceInter {

    private static final Logger _log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private CuteBoyUserMapper cuteBoyUserMapper;

    /**
     * 添加用户
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public int addUser(String username,String password) {
        String serviceCode = UUID.randomUUID().toString().replace("-","").toUpperCase();
        CuteBoyUser user = new CuteBoyUser();
        user.setServicecode(serviceCode);
        user.setUsername(username);
        user.setPassword(password);
        return cuteBoyUserMapper.insertSelective(user);
    }

    /**
     * 查询全部用户
     * @param page 页码
     * @return
     */
    public JSONArray getAllUser(int page,int pageSize) {
        _log.info("asdfasdgasdgasdfgas");
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = null;
        CuteBoyUserExample userExample = new CuteBoyUserExample();
        userExample.setOffset((page-1)*pageSize);
        userExample.setLimit(pageSize);
        List<CuteBoyUser> cuteBoyUserList = cuteBoyUserMapper.selectByExample(userExample);
        if (null != cuteBoyUserList && cuteBoyUserList.size()>0) {
            for (CuteBoyUser user : cuteBoyUserList) {
                jsonObject = new JSONObject();
                jsonObject.put("username",user.getUsername());
                jsonObject.put("password",user.getPassword());
                jsonObject.put("serviceCode",user.getServicecode());
                jsonArray.add(jsonObject);
            }
            return jsonArray;
        }
        return null;
    }

}
